package com.jinghangzz.shop.demo.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jinghangzz.shop.common.service.impl.BaseServiceImpl;
import com.jinghangzz.shop.common.util.ConstatFinalUtil;
import com.jinghangzz.shop.common.util.PageInfoUtil;
import com.jinghangzz.shop.demo.dao.IADynastyDao;
import com.jinghangzz.shop.demo.dao.IAKingDao;
import com.jinghangzz.shop.demo.pojo.ADynasty;
import com.jinghangzz.shop.demo.pojo.AKing;
import com.jinghangzz.shop.demo.service.IDemoService;

/**
 * 用户Service的实现类
 * @author TeaBig
 */
@Service("demoService")
public class DemoServiceImpl extends BaseServiceImpl implements IDemoService
{
	@Resource
	private IADynastyDao dynastyDao ;
	@Resource
	private IAKingDao kingDao;

	@Override
	public ADynasty findOneDynastyService(Map<String, Object> condMap)
	{
		return this.dynastyDao.findOneDao(condMap);
	}

	@Override
	public List<ADynasty> findCondListDynastyService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 让关键字使用模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if(pageInfoUtil != null)
		{
			/* 要分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ADynasty> dynastyList = this.dynastyDao.findCondList(condMap);
			/* 分页两要素:
			 * 共多少条
			 * 每页多少条
			 *  */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return dynastyList ; 
		}
		/* 不分页 */
		return this.dynastyDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveOneDynastyService(ADynasty dynasty) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		
		dynasty.setCreateTime(new Date());
		dynasty.setUpdateTime(new Date());
		int res =  this.dynastyDao.saveOne(dynasty);
		
		if(res > 0 )
		{
			/* 保存成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("0"));
			/* 返回值为JSON */
			JSONObject dataResJSON = new JSONObject();
			dataResJSON.put("effect", res);
			dataResJSON.put("id", dynasty.getId());
			
			resultJSON.put("data", dataResJSON);
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("-1"));
		}
		
		/* 制作一个异常
		 * 期望如果有异常,保存的记录要回滚(事务回滚)
		 *  */
//		try
//		{
////			String str = null ; 
////			str.toString() ;
//			
//			/* 造一个异常,不是runTimeException */
//			int i = 4 / 0 ; 
//		} catch (Exception e)
//		{
//			throw new Exception("aaaa") ; 
//		} 
		return resultJSON ; 
	}

	@Override
	public JSONObject updateOneDynastyService(ADynasty dynasty) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		dynasty.setUpdateTime(new Date());
		int res =  this.dynastyDao.updateOne(dynasty);
		if(res > 0 )
		{
			/* 保存成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("0"));
			/* 返回值为JSON */
			JSONObject dataResJSON = new JSONObject();
			dataResJSON.put("effect", res);
			
			resultJSON.put("data", dataResJSON);
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("-1"));
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject deleteBatchDynastyService(Map<String, Object> condMap) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.dynastyDao.deleteBatch(condMap);
		if(res > 0 )
		{
			/* 保存成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("0"));
			/* 返回值为JSON */
			JSONObject dataResJSON = new JSONObject();
			dataResJSON.put("effect", res);
			
			resultJSON.put("data", dataResJSON);
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("-1"));
		}
		return resultJSON ; 
	} 
	
	@Override
	public AKing findOneKingService(Map<String, Object> condMap)
	{
		return this.kingDao.findOneDao(condMap);
	}

	@Override
	public List<AKing> findCondListKingService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 让关键字使用模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if(pageInfoUtil != null)
		{
			/* 要分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AKing> kingList = this.kingDao.findCondList(condMap);
			/* 分页两要素:
			 * 共多少条
			 * 每页多少条
			 *  */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return kingList ; 
		}
		/* 不分页 */
		return this.kingDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveOneKingService(AKing king) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		king.setCreateTime(new Date());
		king.setUpdateTime(new Date());
		int res =  this.kingDao.saveOne(king);
		/* 制作一个异常
		 * 期望如果有异常,保存的记录要回滚(事务回滚)
		 *  */
//		try
//		{
////			String str = null ; 
////			str.toString() ;
//			
//			/* 造一个异常,不是runTimeException */
//			int i = 4 / 0 ; 
//		} catch (Exception e)
//		{
//			throw new Exception("aaaa") ; 
//		} 
		if(res > 0 )
		{
			/* 保存成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("0"));
			/* 返回值为JSON */
			JSONObject dataResJSON = new JSONObject();
			dataResJSON.put("effect", res);
			dataResJSON.put("id", king.getId());
			
			resultJSON.put("data", dataResJSON);
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("-1"));
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject updateOneKingService(AKing king) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		king.setUpdateTime(new Date());
		int res = this.kingDao.updateOne(king);
		
		if(res > 0 )
		{
			/* 保存成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("0"));
			/* 返回值为JSON */
			JSONObject dataResJSON = new JSONObject();
			dataResJSON.put("effect", res);
			dataResJSON.put("id", king.getId());
			
			resultJSON.put("data", dataResJSON);
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("-1"));
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject deleteBatchKingService(Map<String, Object> condMap) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.kingDao.deleteBatch(condMap);
		
		if(res > 0 )
		{
			/* 保存成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("0"));
			/* 返回值为JSON */
			JSONObject dataResJSON = new JSONObject();
			dataResJSON.put("effect", res);
			
			resultJSON.put("data", dataResJSON);
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString("-1"));
		}
		return resultJSON ; 
	} 
}
